import {BasePlugin} from '../base/base-plugin.js';
import {TerminalStyleManager,TerminalLayoutManager} from './terminal-managers.js';

(function () {
    const SELECTOR = 'widget-terminal-preview-area';

    class TerminalPlugin extends BasePlugin {
        static DEFAULT_CONFIG = {
            ...BasePlugin.DEFAULT_CONFIG,
            SELECTOR: SELECTOR,
            EXTENSION_NAME: "terminal",
            CONSTANTS: {
                CLASS_PREFIX: "terminal-preview",
                PREVIEW_CONTAINER: "terminal-wrapper",
                PREVIEW_TITLE: "terminal-title",
                PREVIEW_CONTENT: "terminal-content",
                CONTROLS_CONFIG: {
                    prompt: {
                        widget: 'Text',
                        event: ["change"],
                        default: '$'
                    },
                    shell: {
                        widget: 'Dropdown',
                        event: ["change"],
                        default: 'bash'
                    },
                    height: {
                        widget: 'IntSlider',
                        event: ["change"],
                        default: 152,
                        scaleFactor: 1,
                        symbol: 'px',
                        toFixed: 1
                    },
                    title: {
                        widget: 'Text',
                        event: ["change"],
                        default: ''
                    },
                    platform: {
                        widget: 'Dropdown',
                        event: ["change"],
                        default: 'windows'
                    },
                    user: {
                        widget: 'Text',
                        event: ["change"],
                        default: 'root'
                    },
                    blink: {widget: 'Checkbox', event: ["change"], default: false},
                    interval: {
                        widget: 'IntSlider',
                        event: ["change"],
                        default: 0.8,
                        scaleFactor: 1,
                        symbol: 's',
                        toFixed: 1
                    },
                    color: {
                        widget: 'ColorPicker',
                        event: ["change", "input"],
                        default: '#62ed8b'
                    },
                    style: {
                        widget: 'Dropdown',
                        event: ["change"],
                        default: 'block'
                    },
                },
                SEP_SYMBOL: "➜",
                TIME_SYMBOL: "",
                PLATFORM_ICONS: {
                    "windows": "",
                    "linux": "",
                    "mac": "",
                },
                SHELL_ICONS: {
                    "bash": "",
                    "zsh": "󰰶",
                    "cmd": "",
                    "powershell": "",
                    "python": "",
                },
            },
            MANAGERS: {
                ...BasePlugin.DEFAULT_CONFIG.MANAGERS,
                style: TerminalStyleManager,
                layout: TerminalLayoutManager,
            }
        };
    }

    function initPreview() {
        const checkReady = setInterval(() => {
            if ($(`.${SELECTOR}`).length > 0) {
                clearInterval(checkReady);
                new TerminalPlugin();
            }
        }, 100);
    }

    if (document.readyState === 'complete') {
        initPreview();
    } else {
        document.addEventListener('DOMContentLoaded', initPreview);
    }
})()